﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
namespace WebTeamplate
{

    public partial class Admin_LanguageDetail : BasePage
    {
        private string validateGroup = "cateDetail";      
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSave.ClientID);           
            if (!Page.IsPostBack)
            {
                InitTextControl();               
                InitParamater();
                LoadData();
            }
        }

        private void LoadData()
        {
            if (txtCode.Text == string.Empty)
                return;

            Language objData = LanguageUtil.Get(txtCode.Text);
            if (objData != null)
            {
                if (objData.Published == CommonFunction.True)
                    chkIsActive.Checked = true;
                txtCode.Text = objData.LanguageId;
                txtName.Text = objData.Name;
            }
            CommonFunction.DisableTextBox(txtCode);
        }        
        private void InitParamater()
        {
            this.txtCode.Text = CommonFunction.GetValueFromQueryString(this.Page, "id");           
            this.Command = CommonFunction.GetValueFromQueryString(this.Page, "act");
        }
        private void InitTextControl()
        {
            //  imgBirthDay.Attributes.Add("onclick", "displayCalendar('../calendar/images/',document.forms[0]." + txtBirthDay.ClientID + ",'" + BaseFunctions.DEFAULT_DATE_FORMAT + "',this)");

            btnSave.Text = Resources.Resource.LABEL_BUTTON_SAVE;
            btnCancel.Text = Resources.Resource.LABEL_BUTTON_CANCEL;
            btnReset.Text = Resources.Resource.LABEL_BUTTON_RESET;
            refvCode.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblCode));
            refvCode.ValidationGroup = validateGroup;
            refvName.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblName));
            refvName.ValidationGroup = validateGroup;
            vsumCategory.ValidationGroup = validateGroup;
            btnSave.ValidationGroup = validateGroup;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Language objData = new Language();
                objData.Published = chkIsActive.Checked ? CommonFunction.True : CommonFunction.False;
                objData.LanguageId = txtCode.Text.Trim();
                objData.Name = txtName.Text.Trim();
                objData.CreatedBy = this.userLoginName;
                objData.CreatedDate = CommonFunction.Now;
                objData.LastUpdatedBy = this.userLoginName;
                objData.LastUpdatedDate = objData.CreatedDate;
                if (this.Command == CommonFunction.EdityCommand)
                    Update(objData);
                else
                    Insert(objData);

            }
            else
            {
                lblErrorMessage.Text = "Data is not valid. Please try again.";
            }
        }

        private void Insert(Language objData)
        {
            if (LanguageUtil.Insert(objData))
            {
                Response.Redirect("Language.aspx?" + GetPageParams(FilterFieldType.All));
            }
            else
            {
                lblErrorMessage.Text = Resources.Resource.MESSAGE_INSERT_ERROR;
            }
        }

        private void Update(Language objData)
        {
            objData.LanguageId = txtCode.Text.Trim();
            if (LanguageUtil.Update(objData))
            {
                Response.Redirect("Language.aspx?" + GetPageParams(FilterFieldType.All));
            }
            else
            {
                lblErrorMessage.Text = Resources.Resource.MESSAGE_UPDATE_ERROR;
            }
        }       
       
        protected void btnReset_Click(object sender, EventArgs e)
        {

        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Language.aspx?" + GetPageParams(FilterFieldType.All));
        }
        public string Command
        {

            get
            {
                object o = ViewState["Command"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["Command"] = value; }

        }        
    }
}
